package cn.iocoder.yudao.module.eval.controller.admin.template;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.eval.controller.admin.template.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.template.EvalTemplateDO;
import cn.iocoder.yudao.module.eval.service.template.EvalTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.framework.common.pojo.PageParam;

@Tag(name = "管理后台 - 评价模板")
@RestController
@RequestMapping("/eval/template")
@Validated
public class EvalTemplateController {

    @Resource
    private EvalTemplateService templateService;

    @PostMapping("/create")
    @Operation(summary = "创建评价模板")
    @PreAuthorize("@ss.hasPermission('eval:template:create')")
    public CommonResult<Long> createTemplate(@Valid @RequestBody EvalTemplateSaveReqVO createReqVO) {
        return success(templateService.createTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新评价模板")
    @PreAuthorize("@ss.hasPermission('eval:template:update')")
    public CommonResult<Boolean> updateTemplate(@Valid @RequestBody EvalTemplateSaveReqVO updateReqVO) {
        templateService.updateTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评价模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:template:delete')")
    public CommonResult<Boolean> deleteTemplate(@RequestParam("id") Long id) {
        templateService.deleteTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得评价模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:template:query')")
    public CommonResult<EvalTemplateRespVO> getTemplate(@RequestParam("id") Long id) {
        EvalTemplateDO template = templateService.getTemplate(id);
        return success(BeanUtils.toBean(template, EvalTemplateRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得评价模板列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('eval:template:query')")
    public CommonResult<List<EvalTemplateRespVO>> getTemplateList(@RequestParam("ids") Collection<Long> ids) {
        List<EvalTemplateDO> list = templateService.getTemplateList(ids);
        return success(BeanUtils.toBean(list, EvalTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得评价模板分页")
    @PreAuthorize("@ss.hasPermission('eval:template:query')")
    public CommonResult<PageResult<EvalTemplateRespVO>> getTemplatePage(@Valid EvalTemplatePageReqVO pageReqVO) {
        PageResult<EvalTemplateDO> pageResult = templateService.getTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvalTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出评价模板 Excel")
    @PreAuthorize("@ss.hasPermission('eval:template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTemplateExcel(@Valid EvalTemplatePageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvalTemplateDO> list = templateService.getTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "评价模板.xls", "数据", EvalTemplateRespVO.class,
                BeanUtils.toBean(list, EvalTemplateRespVO.class));
    }

    @GetMapping("/list-by-profession-and-type")
    @Operation(summary = "根据职业和模板类型获得模板列表")
    @Parameter(name = "profession", description = "适用职业", required = true, example = "数控车工")
    @Parameter(name = "templateType", description = "模板类型", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:template:query')")
    public CommonResult<List<EvalTemplateRespVO>> getTemplateListByProfessionAndType(
            @RequestParam("profession") String profession,
            @RequestParam("templateType") Integer templateType) {
        List<EvalTemplateDO> list = templateService.getTemplateListByProfessionAndType(profession, templateType);
        return success(BeanUtils.toBean(list, EvalTemplateRespVO.class));
    }

    @PutMapping("/publish")
    @Operation(summary = "发布模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:template:publish')")
    public CommonResult<Boolean> publishTemplate(@RequestParam("id") Long id) {
        templateService.publishTemplate(id);
        return success(true);
    }

    @PutMapping("/disable")
    @Operation(summary = "停用模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:template:disable')")
    public CommonResult<Boolean> disableTemplate(@RequestParam("id") Long id) {
        templateService.disableTemplate(id);
        return success(true);
    }

    @PostMapping("/copy")
    @Operation(summary = "复制模板")
    @PreAuthorize("@ss.hasPermission('eval:template:create')")
    public CommonResult<Long> copyTemplate(@Valid @RequestBody EvalTemplateCopyReqVO copyReqVO) {
        return success(templateService.copyTemplate(copyReqVO.getId(), copyReqVO.getNewTemplateCode(), copyReqVO.getNewTemplateName()));
    }

    @PutMapping("/set-default")
    @Operation(summary = "设置默认模板")
    @PreAuthorize("@ss.hasPermission('eval:template:set-default')")
    public CommonResult<Boolean> setDefaultTemplate(@Valid @RequestBody EvalTemplateSetDefaultReqVO setDefaultReqVO) {
        templateService.setDefaultTemplate(setDefaultReqVO.getId(), setDefaultReqVO.getProfession(), setDefaultReqVO.getTemplateType());
        return success(true);
    }

    @GetMapping("/list-published")
    @Operation(summary = "获取已发布的评价模板列表")
    @PreAuthorize("@ss.hasPermission('eval:template:query')")
    public CommonResult<List<EvalTemplateRespVO>> getPublishedTemplateList() {
        List<EvalTemplateDO> list = templateService.getPublishedTemplateList();
        return success(BeanUtils.toBean(list, EvalTemplateRespVO.class));
    }

}
